package com.controller;

import org.springframework.web.bind.annotation.*;
import java.util.*;
import com.utils.R;
import com.annotation.IgnoreAuth;

/**
 * 选课管理控制器
 */
@RestController
@RequestMapping("/selection")
public class SelectionController {

    // 模拟选课数据
    private static List<Map<String, Object>> selectionList = new ArrayList<>();
    
    // 初始化模拟数据
    static {
        for (int i = 1; i <= 10; i++) {
            Map<String, Object> record = new HashMap<>();
            record.put("id", i);
            record.put("studentId", (i % 3) + 1);
            record.put("studentName", new String[]{"张三", "李四", "王五"}[i % 3]);
            record.put("studentNumber", "202300" + ((i % 3) + 1));
            record.put("courseId", (i % 3) + 1);
            record.put("courseName", new String[]{"高等数学", "大学英语", "编程导论"}[i % 3]);
            record.put("semester", new String[]{"第一学期", "第一学期", "第二学期"}[i % 3]);
            record.put("credit", new Integer[]{4, 3, 2}[i % 3]);
            record.put("score", 70 + (int)(Math.random() * 30));
            record.put("selectionTime", new Date().getTime() - i * 86400000);
            selectionList.add(record);
        }
    }

    /**
     * 获取选课列表
     */
    @IgnoreAuth
    @GetMapping("/list")
    public R list(@RequestParam(required = false) Integer page,
                  @RequestParam(required = false) Integer size,
                  @RequestParam(required = false) String studentName,
                  @RequestParam(required = false) String courseName) {
        
        // 过滤数据
        List<Map<String, Object>> filteredList = new ArrayList<>(selectionList);
        if (studentName != null && !studentName.isEmpty()) {
            filteredList.removeIf(item -> !item.get("studentName").toString().contains(studentName));
        }
        if (courseName != null && !courseName.isEmpty()) {
            filteredList.removeIf(item -> !item.get("courseName").toString().contains(courseName));
        }
        
        // 分页
        int total = filteredList.size();
        int pageNo = page == null ? 1 : page;
        int pageSize = size == null ? 10 : size;
        int startIndex = (pageNo - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, total);
        
        List<Map<String, Object>> pageData = new ArrayList<>();
        if (startIndex < total) {
            pageData = filteredList.subList(startIndex, endIndex);
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageData);
        result.put("total", total);
        
        return R.ok().put("data", result);
    }
    
    /**
     * 添加选课记录
     */
    @IgnoreAuth
    @PostMapping("/save")
    public R save(@RequestBody Map<String, Object> selection) {
        selection.put("id", selectionList.size() + 1);
        selection.put("selectionTime", new Date().getTime());
        selectionList.add(selection);
        return R.ok();
    }
    
    /**
     * 更新选课记录
     */
    @IgnoreAuth
    @PostMapping("/update")
    public R update(@RequestBody Map<String, Object> selection) {
        int id = (int) selection.get("id");
        for (int i = 0; i < selectionList.size(); i++) {
            if ((int)selectionList.get(i).get("id") == id) {
                selectionList.set(i, selection);
                return R.ok();
            }
        }
        return R.error("记录不存在");
    }
    
    /**
     * 删除选课记录
     */
    @IgnoreAuth
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") int id) {
        for (int i = 0; i < selectionList.size(); i++) {
            if ((int)selectionList.get(i).get("id") == id) {
                selectionList.remove(i);
                return R.ok();
            }
        }
        return R.error("记录不存在");
    }
} 